﻿//
// GNS_Accounted.cpp
// MobileManageSys
//FuncTion  分成占比
// Created by mr.l on 16/1/15.
//
//
#include "GnsClientFrame.h"
#include "GNS_Accounted.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include <algorithm>
#include "GNS_SceneMgr.h"
#include "GnsOften.h"
#include "SystemMgr.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()
////extern ISocketMgr * _GameSock;
GNS_Accounted::GNS_Accounted()
:_searchText(nullptr)
{
    pGNS_Accounted=0;
    pListView=nullptr;
    InPutText=nullptr;
    //pCursorTEXT 
	searchText =nullptr;
    IDText=nullptr;
    AccountText=nullptr;
    AccountText1=nullptr;
    num=nullptr;
    pOperator=nullptr;
}

GNS_Accounted::~GNS_Accounted()
{
    
}

GNS_Accounted* GNS_Accounted::createLayer()
{
    auto pGNS_Accounted = GNS_Accounted::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_Accounted);
    return pGNS_Accounted;
}

bool GNS_Accounted::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}
void GNS_Accounted::SetNodeName(std::string CsbName)
{
    pGNS_Accounted = CSLoader::createNode(CsbName);
    pGNS_Accounted->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_Accounted);
    addChild(pGNS_Accounted,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_Accounted);
	GNS_NodeMgr::getInstance()->pushChildNode("GNS_Accounted", pGNS_Accounted);

    auto pBackBtn = static_cast<Button*>(pGNS_Accounted->getChildByName("AccBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_Accounted::BtnCallBack, this));
    
    auto pChooesBtn =static_cast<Button *>(pGNS_Accounted->getChildByName("search"));
    pChooesBtn->setTag(2);
    pChooesBtn->addTouchEventListener(CC_CALLBACK_2(GNS_Accounted::BtnCallBack, this));
    
    pListView = static_cast<ListView*>(pGNS_Accounted->getChildByName("ListView_1"));
    
    std::string PlaceHolder ="";
    if (GNS_PlayerMgr::getInstance()->getUserLevel()==0) {
        PlaceHolder = CCGnsOften::getUtf8_DirectAssignmentInProgram("请输入代理账号");
    }else{
		PlaceHolder = CCGnsOften::getUtf8_DirectAssignmentInProgram("请输入推广员账号");
    }
    
    //pCursorTEXT = GNS_SceneMgr::getInstance()->MycreateEditBox(PlaceHolder.c_str(), Vec2(winsize.width*0.28, winsize.height*0.8621), 24, Color3B::BLACK, 10, false);
	searchText = static_cast<TextField*>(pGNS_Accounted->getChildByName("TextField_input"));//TextField_input
     //pGNS_Accounted->addChild(pCursorTEXT,12);
	searchText->setPlaceHolder(PlaceHolder);
	_searchText = GNS_Handler::createTMEditBoxWithExistFeild(searchText, pGNS_Accounted, 20);
    /**
     *  获取界面数据
     */
    auto pImageView = static_cast<ImageView*>(pGNS_Accounted->getChildByName("Image_9"));
    IDText = static_cast<Text*>(pImageView->getChildByName("IDText"));
    AccountText = static_cast<Text*>(pImageView->getChildByName("AccountText"));
    AccountText1 = static_cast<Text*>(pImageView->getChildByName("AccountText1"));
    num = static_cast<Text*>(pImageView->getChildByName("num"));
    if (GNS_PlayerMgr::getInstance()->getUserLevel()==0) {
        auto AccTitleText = static_cast<Text*>(pGNS_Accounted->getChildByName("Text_5"));
        AccTitleText->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("代理商-分成占比"));
        IDText->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("代理账号"));
        AccountText->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("运营占比"));
        AccountText1->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("该代理及下限占比"));
        num->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("推广员数量"));
    }
}
void GNS_Accounted::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                this->removeAllChildrenWithCleanup(true);
                this->getParent()->removeChild(this);
				}

				//GNS_NodeMgr::getInstance()->eraseChildNode("GNS_Accounted");
                //auto PromoterNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(1);
                //PromoterNode->setVisible(true);
				GNS_NodeMgr::getInstance()->setMenuVisible("Promoter", true);
				
                break;
            }
            case 2:
            {
				std::string textstr = _searchText->getText();
                log("%s",textstr.c_str());
                int Input =atoi(textstr.c_str());
                if (textstr.length()!=0) {
                    bool IsSearch =false;
                    int x=0;
                    for (auto it:*(GNS_PlayerMgr::getInstance()->getAgProPer())) {
                        if (it._lowlineCode==Input) {
                            IsSearch=true;
                            pListView->removeAllItems();
                            _AgProPer.clear();
                            _AgProPer.push_back(it);
                            RefreshList(it,x);
                        }
                        x++;
                    }
                    if (IsSearch==false) {
                        Prompt();
                        SetAccDate();
                    }
                }else{
                    Prompt();
                    SetAccDate();
                }
                break;
            }
            default:
                break;
        }
    }
}
void GNS_Accounted::SetAccDate()
{

    for (auto it:*(GNS_PlayerMgr::getInstance()->getAgProPer())) {
        _AgProPer.push_back(it);
    }
    pListView->removeAllItems();
    int x=0;
    int index =(int) _AgProPer.size();
    Size bgSize;
	int user = GNS_PlayerMgr::getInstance()->getUserLevel();
	string filename = "";
	if (user == 0)
		filename = "line between1.png";
	else
		filename = "line2.png";
    for (auto it:_AgProPer) {


        auto  pImageBg = Button::create(filename);
        pImageBg->setTag(100+x);
        if(GNS_PlayerMgr::getInstance()->getUserLevel()==0)
        {
            pImageBg->addTouchEventListener(CC_CALLBACK_2(GNS_Accounted::ListViewBtn, this));
        }
        pListView->addChild(pImageBg);
        
        bgSize = pImageBg->getContentSize();
        auto ID  = Text::create(StringUtils::format("%d",it._lowlineCode), GameFontName, 24);
        ID->setPosition(Vec2(bgSize.width*0.11, bgSize.height*0.4));
        ID->setColor(Color3B::BLACK);
        pImageBg->addChild(ID);
        
        auto agentPercent =Text::create(StringUtils::format("%d",it._agentPercent), GameFontName, 24);
        agentPercent->setPosition(Vec2(bgSize.width*0.35, bgSize.height*0.4));
        agentPercent->setColor(Color3B::BLACK);
        pImageBg->addChild(agentPercent);
        
        auto promotePercent = Text::create(StringUtils::format("%d",it._lowlinePercent), GameFontName, 24);
        promotePercent->setPosition(Vec2(bgSize.width*0.58, bgSize.height*0.4));
        promotePercent->setColor(Color3B::BLACK);
        pImageBg->addChild(promotePercent);
        
        auto playerNum = Text::create(StringUtils::format("%d",it._memberNum), GameFontName, 24);
        playerNum->setPosition(Vec2(bgSize.width*0.87, bgSize.height*0.4));
        playerNum->setColor(Color3B::BLACK);
        pImageBg->addChild(playerNum);
        x++;
    }
    if (bgSize.width!=0||bgSize.height!=0) {
        pListView->setInnerContainerSize(Size(bgSize.width, (bgSize.height+10)*index));
    }
}

void GNS_Accounted::SelectedItemEvents(cocos2d::Ref *pSender, ui::ListView::EventType type)
{
    if (type==ui::ListView::EventType::ON_SELECTED_ITEM_START) {
        if (GNS_PlayerMgr::getInstance()->getUserLevel()==0) {
            auto listview = static_cast<ListView*>(pSender);
            CC_UNUSED_PARAM(listview);
            int Index = (int)listview->getCurSelectedIndex();
            int x=0;
            for (auto it:_AgProPer) {
                if (x==Index) {
                    int m_lowlineCode=it._lowlineCode;
                    CMD_CS_GetAgentProfitPercent pExList;
                    pExList._agentCode=m_lowlineCode;
                    ////_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_GET_SINGLE_AGENT_PERCENT, &pExList, sizeof(pExList));
                    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN,SUB_CS_GET_SINGLE_AGENT_PERCENT,(char*)&pExList,sizeof(pExList));
                    pGNS_Accounted->setVisible(false);
                    pOperator=GNS_Operator::createLayer();
                    pOperator->SetNodeName("OperatorAgent.csb",m_lowlineCode);
                }
                x++;
            }
        }
    }
}
void GNS_Accounted::RefreshList(CMD_SC_GetAgentProfitPercentResult it,int x)
{
    auto  pImageBg = Button::create("line2.png","line1.png");
    if (GNS_PlayerMgr::getInstance()->getUserLevel()==0) {
        pImageBg->addTouchEventListener(CC_CALLBACK_2(GNS_Accounted::ListViewBtn, this));
    }
    pImageBg->setTag(100+x);
    pListView->addChild(pImageBg);
    
    Size  bgSize = pImageBg->getContentSize();
    auto ID  = Text::create(StringUtils::format("%d",it._lowlineCode), GameFontName, 24);
    ID->setPosition(Vec2(bgSize.width*0.11, bgSize.height*0.28));
    ID->setColor(Color3B::BLACK);
    pImageBg->addChild(ID);
    
    auto agentPercent =Text::create(StringUtils::format("%d",it._agentPercent), GameFontName, 24);
    agentPercent->setPosition(Vec2(bgSize.width*0.35, bgSize.height*0.28));
    agentPercent->setColor(Color3B::BLACK);
    pImageBg->addChild(agentPercent);
    
    auto promotePercent = Text::create(StringUtils::format("%d",it._lowlinePercent), GameFontName, 24);
    promotePercent->setPosition(Vec2(bgSize.width*0.58, bgSize.height*0.28));
    promotePercent->setColor(Color3B::BLACK);
    pImageBg->addChild(promotePercent);
    
    auto playerNum = Text::create(StringUtils::format("%d",it._memberNum), GameFontName, 24);
    playerNum->setPosition(Vec2(bgSize.width*0.87, bgSize.height*0.28));
    playerNum->setColor(Color3B::BLACK);
    pImageBg->addChild(playerNum);

}

void GNS_Accounted::Prompt()
{
    auto pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
    pPopWnd->setClickWndOutsideClose(true);
    pPopWnd->SetTitleWithUTF("提示!");
    pPopWnd->SetContentTextWithUTF("没有搜索到", Color3B::BLACK);
}
void GNS_Accounted::ListViewBtn(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        int x=0;
        for (auto it:_AgProPer) {
            if (x==BtnTag-100) {
                int m_lowlineCode=it._lowlineCode;
                CMD_CS_GetAgentProfitPercent pExList;
                pExList._agentCode=m_lowlineCode;
               // GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_SINGLE_AGENT_PERCENT, &pExList, sizeof(pExList));
                ////_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_GET_SINGLE_AGENT_PERCENT, &pExList, sizeof(pExList));
                 GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN,SUB_CS_GET_SINGLE_AGENT_PERCENT, (char*)&pExList, sizeof(pExList));
                pGNS_Accounted->setVisible(false);
                pOperator=GNS_Operator::createLayer();
                pOperator->SetNodeName("OperatorAgent.csb",m_lowlineCode);
            }
            x++;
        }
    }
}